/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.inf.ufg.model;

import br.inf.ufg.factory.ConexaoFactoryMySql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
/**
 *
 * @author umarley.faria
 */
public class AreasDAO {
    
    private final Connection con;
    private final Statement stmt;
    private final Statement stmtNavegar;
    private ResultSet rsNavegar;
    
    public AreasDAO() throws ClassNotFoundException, SQLException{        
         
             con = ConexaoFactoryMySql.getConnection();
             stmt = con.createStatement();
             stmtNavegar = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,
                     ResultSet.CONCUR_UPDATABLE );
             rsNavegar = stmtNavegar.executeQuery( "SELECT * FROM tiposassentos" );        
    
    }
    
    public Areas primeiro() throws SQLException {
        
            if (rsNavegar.first() ) {            
                Areas areas = new Areas();
                areas.setEvento(rsNavegar.getInt( "EVENTO" ) );
                areas.setCodigo(rsNavegar.getString( "CODIGO" ) );
                areas.setValor(rsNavegar.getDouble( "VALOR" ) );
                areas.setDescricao(rsNavegar.getString( "DESCRICAO" ) );
                areas.setQuantidade(rsNavegar.getInt( "QUANTIDADE" ) );
                
                return areas;
                
            } else {
                return null;
            }
        
    }
    
    public Areas anterior() throws SQLException {
        
            if ( ! rsNavegar.isFirst() ) {
                rsNavegar.previous();
                Areas areas = new Areas();
                areas.setEvento(rsNavegar.getInt( "EVENTO" ) );
                areas.setCodigo(rsNavegar.getString( "CODIGO" ) );
                areas.setValor(rsNavegar.getDouble( "VALOR" ) );
                areas.setDescricao(rsNavegar.getString( "DESCRICAO" ) );
                areas.setQuantidade(rsNavegar.getInt( "QUANTIDADE" ) );
                
                return areas; 
                
            } else {
                return null;
            }
        
    }
    
    
    public Areas proximo() throws SQLException {
        
            if ( ! rsNavegar.isLast() ) { 
                rsNavegar.next();                
                Areas areas = new Areas();
                areas.setEvento(rsNavegar.getInt( "EVENTO" ) );
                areas.setCodigo(rsNavegar.getString( "CODIGO" ) );
                areas.setValor(rsNavegar.getDouble( "VALOR" ) );
                areas.setDescricao(rsNavegar.getString( "DESCRICAO" ) );
                areas.setQuantidade(rsNavegar.getInt( "QUANTIDADE" ) );
                return areas; 
                
            } else {
                return null;
            }       
     }

     
     
      public Areas ultimo() throws SQLException {
        
            if ( rsNavegar.last() ) {               
                Areas areas = new Areas();
                areas.setEvento(rsNavegar.getInt( "EVENTO" ) );
                areas.setCodigo(rsNavegar.getString( "CODIGO" ) );
                areas.setValor(rsNavegar.getDouble( "VALOR" ) );
                areas.setDescricao(rsNavegar.getString( "DESCRICAO" ) );
                areas.setQuantidade(rsNavegar.getInt( "QUANTIDADE" ) );
                return areas;                 
            } else {
                return null;
            }  
            
      }
                
      
      public boolean excluirRegistro( int evento, String codigo ) throws SQLException {
      
            stmt.executeUpdate( "DELETE FROM areas WHERE EVENTO = " + evento + "AND CODIGO =" +codigo);
            rsNavegar = stmtNavegar.executeQuery( "SELECT * FROM areas" );
            return true;        
      }
      
      public boolean salvarRegistro( Areas areas ) throws SQLException, Exception{
          
          int evento = areas.getEvento();
          String codigo = areas.getCodigo();
          Double valor = areas.getValor();
          String descricao = areas.getDescricao();
          int quantidade = areas.getQuantidade();
          
        try {
            stmt.executeUpdate( "INSERT INTO `areas` (`EVENTO`, `CODIGO`, "
                    + "`VALOR`, `DESCRICAO`, `QUANTIDADE`) "
                    + "VALUES ("+evento+", '"+codigo+"', "+valor+", '"+descricao+"', "+quantidade+");");
            rsNavegar = stmtNavegar.executeQuery( "SELECT * FROM areas" );
            return true;
            
        } catch ( SQLException e) {
            throw new Exception("Erro ao inserir dados. Tente novamente " + e.getMessage());
            
        }
          
      }
      
      
      public ArrayList<Areas> Areas() throws SQLException {
          
          Statement stmtListar = con.createStatement();
          ResultSet rsListar = stmtListar.executeQuery( "SELECT * FROM areas");
          
          ArrayList<Areas> retorno = new ArrayList<Areas>();
            
            while ( rsListar.next() ) {
                int evento = rsListar.getInt("EVENTO" );
                String codigo = rsListar.getString( "CODIGO" );
                Double valor = rsListar.getDouble("VALOR");
                String descricao = rsListar.getString( "DESCRICAO" );
                int quantidade = rsListar.getInt("QUANTIDADE" ); 
                
                
                Areas areas = new Areas();
                areas.setEvento(evento );
                areas.setCodigo(codigo );
                areas.setValor(valor );
                areas.setDescricao(descricao );
                areas.setQuantidade(quantidade );
                
                retorno.add( areas );
            }
            
            return retorno;
      }
      
    
    
}
